﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using SKYPE4COMLib;
using SkypeStudio.Framework;

namespace SkypeStudio.Controls.Model
{
    public class Chat : NotifyPropertyChangedBase
    {
        private ObservableCollection<Contact> _Members;
        public ObservableCollection<Contact> Members
        {
            get
            {
                return _Members;
            }
            set
            {
                if (_Members == value)
                    return;
                if (_Members != null)
                    _Members.CollectionChanged -= UserCollectionChanged;

                _Members = value;

                _Members.CollectionChanged += UserCollectionChanged;
            }
        }
        private ObservableCollection<ChatMessage> _ChatMessages;
        public ObservableCollection<ChatMessage> ChatMessages
        {
            get
            {
                return _ChatMessages;
            }
            set
            {
                if (_ChatMessages == value)
                    return;

                if (_ChatMessages != null)
                    _ChatMessages.CollectionChanged -= ChatMessagesCollectionChanged;

                _ChatMessages = value;

                _ChatMessages.CollectionChanged += ChatMessagesCollectionChanged;
            }
        }

        void UserCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            NotifyOfPropertyChange(() => MembersName);
        }

        void ChatMessagesCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            NotifyOfPropertyChange(() => LastChatMessage);
        }
        public string ChatName { get; set; }

        public string MembersName
        {
            get
            {
                if (Members == null || Members.Count <= 0)
                    return string.Empty;

                StringBuilder sb = new StringBuilder();
                foreach (var item in Members.Where(o=>o.Handle != CurrentUser.Handle))
                {
                    sb.Append(string.Format( "{0}," ,item.Name));
                }
                sb.Remove(sb.Length - 1, 1);
                return sb.ToString();
            }
        }
        
        public string LastChatMessage 
        {
            get 
            {
                if (ChatMessages == null || ChatMessages.Count <= 0)
                    return string.Empty;

                return ChatMessages.Last().Message;
            }
        }

        public OnlineStatus OnlineStatus 
        {
            get
            {
                var allstatus = Members.Where(o=>o.Handle != this.CurrentUser.Handle).Select(o => o.OnlineStatus).Distinct();
                return allstatus.Any(o=>o == Model.OnlineStatus.olsOnline) ? OnlineStatus.olsOnline : Model.OnlineStatus.olsOffline;
            }
        }


        public Contact CurrentUser { get; set; }
    }
}
